package com.fcy.teamwork.business

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.fcy.teamwork.R
import com.fcy.teamwork.bean.User
import com.fcy.teamwork.business.projectlife.ProjectControllerVM
import com.fcy.teamwork.databinding.BottomSheetFragmentAddNewSkillBinding
import com.fcy.teamwork.databinding.SelectSomeoneFromGroupBinding
import com.fcy.teamwork.pojo.Person
import com.fcy.teamwork.pojo.TeamWorkResult
import com.fcy.teamwork.repo.RetrofitClient
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.launch
import retrofit2.http.GET
import retrofit2.http.Query

class SelectFromGroupDiaFragment : BottomSheetDialogFragment() {
    private val vm by activityViewModels<ProjectControllerVM>()
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return BottomSheetDialog(requireContext(), R.style.BottomSheetStyle)
    }

    private val selectFromGroupListAdapter by lazy {
        SelectFromGroupListAdapter(isHideCheckBox).apply {
            lifecycleScope.launch {
                submitData(
                    vm.loadAllMember(groupId)
                )
            }
        }
    }

    private val isHideCheckBox: Boolean
        get() = arguments?.getBoolean(arguments_isHideCheckBox) ?: throw IllegalArgumentException(
            "为传入正确的groupId"
        )

    private val groupId: String
        get() = arguments?.getString(arguments_groupId)
            ?: throw IllegalArgumentException("为传入正确的groupId")

    companion object {
        const val arguments_groupId = "group_id"
        const val arguments_isHideCheckBox = "isHideCheckBox"
    }

    private lateinit var binding: SelectSomeoneFromGroupBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = SelectSomeoneFromGroupBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.rvMembers.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = selectFromGroupListAdapter
        }
    }

    override fun onCancel(dialog: DialogInterface) {
        super.onCancel(dialog)
        vm.selectedMembers = selectFromGroupListAdapter.getSelectedMembers()
    }
}